﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Shlook.Common;
using System.Runtime.InteropServices;

namespace Shlook.GUI
{
    public partial class ChatWindowForm : Form
    {
        public ChatWindowForm()
        {
            InitializeComponent();
        }

        private User _talkingTo;

        public ChatWindowForm(User talkingTo) : this()
        {
            _talkingTo = talkingTo;
        }

        public event EventHandler<SendMessageEventArgs> MessageReady = delegate { };
        public event EventHandler<EventArgs> WindowClosed = delegate { };

        private void sendButton_Click(object sender, EventArgs e)
        {
            MessageReady(this, new SendMessageEventArgs { Message = message.Text, To = _talkingTo });
            history.AppendText("me: " + message.Text + Environment.NewLine + Environment.NewLine);
            message.Text = String.Empty;
        }

        private void ChatWindowForm_Load(object sender, EventArgs e)
        {
            Icon = IconProvider.ApplicationIcon;
            Text = "Chat with " + _talkingTo.DisplayName + " (" + _talkingTo.Status + ")";
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            WindowClosed(this, EventArgs.Empty);
        }

        [DllImport("user32.dll")]
        private static extern int FlashWindow(IntPtr hwnd, int bInvert);

        public void AddMessage(string message)
        {
            history.AppendText(_talkingTo.DisplayName + ": " + message + Environment.NewLine + Environment.NewLine);
            FlashWindow(Handle, 0);
        }

        public void UserDisconnected()
        {
            history.AppendText("*** " + _talkingTo.DisplayName + " has disconnected from the server ***" + Environment.NewLine + Environment.NewLine);
        }

        public void UserConnected()
        {
            history.AppendText("*** " + _talkingTo.DisplayName + " has connected to the server ***" + Environment.NewLine + Environment.NewLine);
        }

        private void changeOrientation_Click(object sender, EventArgs e)
        {
            if (changeOrientation.Text == "RTL")
            {
                changeOrientation.Text = "LTR";
                history.RightToLeft = RightToLeft.Yes;
            }
            else
            {
                changeOrientation.Text = "RTL";
                history.RightToLeft = RightToLeft.No;
            }
        }

        public void StatusChanged(string newStatus)
        {
            history.AppendText("*** " + _talkingTo.DisplayName + " changed status to " + newStatus + " ***" + Environment.NewLine + Environment.NewLine);
            Text = "Chat with " + _talkingTo.DisplayName + " (" + newStatus + ")";
        }

        public void ShowUp()
        {
            if (WindowState == FormWindowState.Minimized)
            {
                WindowState = FormWindowState.Normal;
            }
            Activate();
        }
    }

    public class SendMessageEventArgs : EventArgs
    {
        public string Message { get; set; }
        public User To { get; set; }
    }
}
